package edu.lingnan.reactproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.lingnan.reactproject.mapper.RoleMenusMapper;
import edu.lingnan.reactproject.mapper.RolesMapper;
import edu.lingnan.reactproject.pojo.RoleMenus;
import edu.lingnan.reactproject.pojo.Roles;
import edu.lingnan.reactproject.pojo.Users;
import edu.lingnan.reactproject.mapper.UsersMapper;
import edu.lingnan.reactproject.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.lingnan.reactproject.vo.*;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈世杰
 * @since 2022-01-15
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired(required = false)
    private UsersMapper usersMapper;

    @Autowired(required = false)
    private RolesMapper rolesMapper;

    @Autowired(required = false)
    private RoleMenusMapper roleMenusMapper;

    @Override
    public Result add(AddUserParams user) {
        Users userEntity = new Users();
        userEntity.setUsername(user.getUsername());
        userEntity.setPassword(user.getPassword());
        userEntity.setEmail(user.getEmail());
        userEntity.setPhone(user.getPhone());
        userEntity.setRoleId(Integer.parseInt(user.getRole_id()));
        int insert = usersMapper.insert(userEntity);
        if (insert>0){
            QueryWrapper<Users> wrapper = new QueryWrapper<>();
            wrapper.eq("username",user.getUsername());
            Users users = usersMapper.selectOne(wrapper);
            return new Result(0,users);
        }else {
            return new Result(1,"此用户已存在");
        }
    }

    @Override
    public Result update(UpdateUserParams user) {
        Users userEntity = new Users();
        userEntity.setUsername(user.getUsername());
        userEntity.setEmail(user.getEmail());
        userEntity.setPhone(user.getPhone());
        userEntity.setRoleId(Integer.parseInt(user.getRole_id()));
        userEntity.setId(Integer.valueOf(user.get_id()));
        int i = usersMapper.updateById(userEntity);
        if (i>=0){
            return new Result(0,userEntity);
        }else{
            return new Result(1,"此用户不存在");
        }
    }

    @Override
    public Result getUserList() {
        List<Users> users = usersMapper.selectList(null);
        List<Roles> roles = rolesMapper.selectList(null);
        List<RoleMenus> menus = roleMenusMapper.selectList(null);
        List<RolesAndMenu> rolesAndMenuList = new ArrayList<>();
        roles.forEach(role->{
            RolesAndMenu rolesAndMenu = new RolesAndMenu();
            String menuStrings = menus.stream().filter(menu -> menu.getRoleMenuId().equals(role.getId())).toString();
            rolesAndMenu.setMenus(menuStrings);
            rolesAndMenu.setId(role.getId());
            rolesAndMenu.setAuthName(role.getAuthName());
            rolesAndMenu.setName(role.getName());
            rolesAndMenu.setCreateTime(role.getCreateTime());
            rolesAndMenuList.add(rolesAndMenu);
        });
        return new Result(0,new GetUserListResult(users,rolesAndMenuList));
    }

    @Override
    public Result deleteUser(String userId) {
        int i = usersMapper.deleteById(userId);
        if (i>=0){
            return new Result(0,"删除成功");
        }else{
            return new Result(1,"删除失败");
        }
    }
}
